﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HenIT.Data.SqlClient;
using System.Data.SqlClient;
using System.Data;
namespace EventScavenger
{
	public partial class EventScavengerDAL : GenericSQLServerDAL
	{
		#region Collectors
		internal List<EventScavengerUser> GetUserList()
		{
			List<EventScavengerUser> users = new List<EventScavengerUser>();
			string sql = "SecurityListCollectorUsers";
			DataSet userDS = GetDataSet(sql, CommandType.StoredProcedure);
			if (userDS.Tables.Count > 0 && userDS.Tables[0].Rows.Count > 0)
			{
				users.AddRange(
							(from DataRow r in userDS.Tables[0].Rows
							 select new EventScavengerUser()
							 {
								 UserType = UserType.Collector,
								 UserName = r[0].ToString()
							 }
							 )
						 );
			}
			sql = "SecurityListAdmins";
			userDS = GetDataSet(sql, CommandType.StoredProcedure);
			if (userDS.Tables.Count > 0 && userDS.Tables[0].Rows.Count > 0)
			{
				users.AddRange(
							(from DataRow r in userDS.Tables[0].Rows
							 select new EventScavengerUser()
							 {
								 UserType = UserType.Admin,
								 UserName = r[0].ToString()
							 }
							 )
						 );
			}
			sql = "SecurityListViewers";
			userDS = GetDataSet(sql, CommandType.StoredProcedure);
			if (userDS.Tables.Count > 0 && userDS.Tables[0].Rows.Count > 0)
			{
				users.AddRange(
							(from DataRow r in userDS.Tables[0].Rows
							 select new EventScavengerUser()
							 {
								 UserType = UserType.Viewer,
								 UserName = r[0].ToString()
							 }
							 )
						 );
			}
			return users;
		}
		internal void AddUser(EventScavengerUser user)
		{
			string sql;
			if (user.UserType == UserType.Collector)
			{
				sql = "SecurityAddCollectorUser";
			}
			else if (user.UserType == UserType.Admin)
			{
				sql = "SecurityAddAdmin";
			}
			else
				sql = "SecurityAddViewer";
			SqlParameter[] parameters = new SqlParameter[] 
				{
					new SqlParameter("@UserName", user.UserName)
				};
			ExecuteSP(sql, parameters);
		}
		internal void DeleteUser(EventScavengerUser user)
		{
			string sql;
			if (user.UserType == UserType.Collector)
			{
				sql = "SecurityDropCollectorUser";
			}
			else if (user.UserType == UserType.Admin)
			{
				sql = "SecurityDropAdmin";
			}
			else
				sql = "SecurityDropViewer";
			SqlParameter[] parameters = new SqlParameter[] 
				{
					new SqlParameter("@UserName", user.UserName)
				};
			ExecuteSP(sql, parameters);
		}
		internal void ReInitializeDBRoles()
		{
			string sql = "SecurityRestoreRoles";
			ExecuteSP(sql);
		}
		#endregion
	}
}
